/****************************************************************
 * <strong>MFConvert数据转换类</strong>
 * <p>
 * 数据转换类
 * </p>
 *
 * @author AndyZhang 2011
 * @version 0.1
 * ToDo:
 ***************************************************************/

package com.thinkwide.data.core;

import java.math.BigDecimal;

public class MFConvert {

    public static Boolean strToBoolean(String value) {

        return Boolean.parseBoolean(value);

    }

    public static boolean strToBooleanDef(String value, boolean def) {
        boolean b = def;
        try {
            if (MFString.isNullOrEmpty(value)) {
                return b;
            }
            b = strToBoolean(value);

        } catch (Exception e) {
            return b;
        }
        return b;

    }

    public static int strToInt(String value) {

        return Integer.parseInt(value);

    }

    public static String BigDecimalToStr(BigDecimal value) {

        return value.toPlainString();

    }

    public static BigDecimal strToBigDecimal(String value) {
        Double val = Double.valueOf(value);
        return BigDecimal.valueOf(val);

    }

    public static BigDecimal strToBigDecimalDef(String value, double def) {
        double d = strToDoubleDef(value, def);
        return BigDecimal.valueOf(d);
    }

    public static String DoubleToStr(double value) {

        return Double.toString(value);

    }

    public static double strToDouble(String value) {

        return Double.valueOf(value);

    }

    public static double strToDoubleDef(String value, double def) {
        double d = def;
        try {
            d = Double.valueOf(value);
            return d;
        } catch (Exception e) {

        }
        return d;
    }

    public static float strToFloatDef(String value, float def) {
        float f = def;
        try {
            f = Float.parseFloat(value);
            return f;
        } catch (Exception e) {

        }
        return f;

    }

    public static float strToFloat(String value) {

        return Float.parseFloat(value);

    }

    public static int strToIntDef(String value, int def) {
        int i = def;
        if (MFString.isNullOrEmpty(value)) {
            return def;
        }

        try {
            i = Integer.parseInt(value);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return i;
    }

    public static int IntToIntDef(Integer value, int def) {
        if (value == null) {
            return def;
        } else {
            return value;
        }
    }

    /* 一个将字节转化为十六进制ASSIC码的函数 */
    public static String byteHEX(byte ib) {
        char[] Digit = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a',
                'b', 'c', 'd', 'e', 'f'};
        char[] ob = new char[2];
        ob[0] = Digit[(ib >>> 4) & 0X0F];
        ob[1] = Digit[ib & 0X0F];
        String s = new String(ob);
        return s;
    }

    public static float ObjectToFloat(Object value, float def) {
        if (value != null) {
            return def;
        } else {
            try {
                float v = (Float) value;
                return v;
            } catch (Exception ex) {
                return def;
            }
        }

    }

    public static String ObjectToString(Object value, String def) {
        if (value != null) {
            return def;
        } else {
            try {
                String v = (String) value;
                return v;
            } catch (Exception ex) {
                return def;
            }
        }
    }

    public static Double ObjectToDouble(Object value, Double def) {
        if (value != null) {
            return def;
        } else {
            try {
                Double v = (Double) value;
                return v;
            } catch (Exception ex) {
                return def;
            }
        }
    }

    public static Integer ObjectToInt(Object value, Integer def) {
        if (value != null) {
            return def;
        } else {
            try {
                Integer v = (Integer) value;
                return v;
            } catch (Exception ex) {
                return def;
            }
        }
    }

}
